import {reactive} from "vue";
import axios from "axios";
import {routerImports} from '../router'


const user = reactive({
	isLogin: false,
	login: (router) => {
		const createRoute = (parent, arr) => {
			arr.forEach((item) => {
				item.component = routerImports[item.name];
				if (parent) {
					router.addRoute(parent, item);
				} else {
					router.addRoute(item);
				}
				if (item.children) {
					createRoute(item.name, item.children);
				}
			});
		}
		let getUserInfo = () => {
			axios.get("/api/user/getPower").then((resp) => {
				console.log("resp.data.data:",resp.data.data);
				createRoute(null, resp.data.data);
				user.setMenu(resp.data.data);
			})
		}
		getUserInfo();
		user.isLogin = true;
	},
	logout: () => {
		user.isLogin = false;
	},
	menu: [],
	clear: () => {
		user.menu = null;
	},
	addMenu: (item) => {
		user.menu.push(item);
	},
	setMenu: (m) => {
		user.menu = m;
	}
});

export default function useUser(){
	return user;
}